package com.wlyuan.open.assembler;

import com.wlyuan.basic.dto.goods.*;
import com.wlyuan.open.basic.dto.*;
import com.wlyuan.utils.Sort;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GoodsAssembler {
    @Mapper(uses = {

    })
    public interface Assembler {

        GoodsTypeCreationDTO assemble(OpenGoodsTypeCreationDTO source);

        @Mapping(target = "name", source = "newName")
        GoodsTypeUpdateDTO assemble(OpenGoodsTypeRenameDTO source);

        GoodsCreationDTO assemble(OpenGoodsCreationDTO source);

        GoodsUpdateDTO assemble(OpenGoodsUpdateDTO source);
    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static GoodsTypeCreationDTO assemble(OpenGoodsTypeCreationDTO source) {
        var detail = ASSEMBLER.assemble(source);
        return detail;
    }

    public static GoodsTypeUpdateDTO assemble(Long goodsTypeId, OpenGoodsTypeRenameDTO source) {
        var detail = ASSEMBLER.assemble(source);
        detail.setGoodsTypeId(goodsTypeId);
        return detail;
    }

    public static GoodsCreationDTO assemble(OpenGoodsCreationDTO source) {
        var creation = ASSEMBLER.assemble(source);
        return creation;
    }

    public static GoodsUpdateDTO assemble(OpenGoodsUpdateDTO source) {
        var update = ASSEMBLER.assemble(source);
        return update;
    }

    public static GoodsFilterDTO assemble(OpenGoodsLatestDTO source) {
        var filter = new GoodsFilterDTO();
        filter.setPage(1L);
        filter.setSize(1L);
        if(StringUtils.isNotBlank(source.getBarcode())) {
            filter.setBarcodes(Arrays.asList(source.getBarcode()));
        }
        if(StringUtils.isNotBlank(source.getName())) {
            filter.setGoodsNames(Arrays.asList(source.getName()));
        }
        List<Sort> sorts = new ArrayList<>(1);
        //filter.setStatus(NamedStatus.ENABLED.getValue());
        Sort invalidSort = new Sort();
        invalidSort.setName("status");
        invalidSort.setAsc(false);
        Sort createSort = new Sort();
        createSort.setName("id");
        createSort.setAsc(false);
        sorts.add(invalidSort);
        sorts.add(createSort);
        filter.setSort(sorts);
        filter.setTenantId(source.getTenantId());
        return filter;
    }


}
